<!-- 个人信息-重置密码 子路由组件-elementUI的Card 卡片实现页面制作 -->
<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>重置密码</span>
      </div>
      <!-- 表单(1.rules 属性传入约定的验证规则 2.ref 操作Dom 3.model表单提交) -->
      <el-form :model="pwdForm" :rules="pwdFormRules" ref="pwdFormRef" label-width="100px" class="demo-userForm">
        <el-form-item label="原密码" prop="old_pwd">
          <el-input v-model="pwdForm.old_pwd" type="password"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="new_pwd">
          <el-input v-model="pwdForm.new_pwd" type="password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="re_pwd">
          <el-input v-model="pwdForm.re_pwd" type="password"></el-input>
        </el-form-item>
        <el-form-item size="large">
          <el-button type="primary" @click="updatePwdFn">修改密码</el-button>
          <el-button @click="reseFn">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { updatePwdAPI } from '@/api/index'
export default {
  name: 'my-userpwd',
  data() {
    // rules规则对象 value关联值 callback回调
    // 1.新密码和旧密码不能相同
    const samePwd = (rules, value, callback) => {
      if (value === this.pwdForm.old_pwd) {
        callback(new Error('新密码不能与原密码一致!'))
      } else {
        callback()
      }
    }
    // 2.确认密码和新密码保持一致
    const rePwd = (rules, value, callback) => {
      if (value !== this.pwdForm.new_pwd) {
        // 如果验证失败，则调用 回调函数时，指定一个 Error 对象。
        callback(new Error('两次新密码不一致！'))
      } else {
        // 如果验证成功，则直接调用 callback 回调函数即可。
        callback()
      }
    }
    return {
      // 表单的数据对象
      pwdForm: {
        old_pwd: '',
        new_pwd: '',
        re_pwd: ''
      },
      // 表单的验证规则对象
      pwdFormRules: {
        old_pwd: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码长度必须是6-15位的非空字符串', trigger: 'blur' }
        ],
        new_pwd: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码长度必须是6-15位的非空字符串', trigger: 'blur' },
          { validator: samePwd, trigger: 'blur' }
        ],
        re_pwd: [
          { required: true, message: '请再次确认新密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码长度必须是6-15位的非空字符串', trigger: 'blur' },
          { validator: rePwd, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 1.修改密码-》点击事件
    updatePwdFn() {
      this.$refs.pwdFormRef.validate(async valid => {
        if (valid) {
          // (1).调用重置密码的接口
          const { data: res } = await updatePwdAPI(this.pwdForm)
          // (2).失败,提示用户
          if (res.code !== 0) {
            return this.$message.error({
              message: `${res.message}`,
              duration: 2000,
              customClass: 'windthClass'
            })
          }
          // (3).成功,提示用户
          this.$message.success({
            message: `${res.message}`,
            duration: 2000,
            customClass: 'windthClass'
          })
          this.$refs.pwdFormRef.resetFields()
          // (4).跳转到登录页面(清除vuex,跳转到登录页面)
          this.$store.commit('updateToken', '')
          this.$store.commit('updateUserInfo', {})
          this.$router.push('/login')
        } else {
          // 阻止默认的提交行为(表单下面的红色提示会自动出现)
          return false
        }
      })
    },
    // 2.重置-》点击事件
    reseFn() {
      // 重置表单的数据且还重置了校验报错的提示
      this.$refs.pwdFormRef.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.clearfix span {
  font-size: 16px;
  font-weight: 700;
}
.clearfix:before,
.clearfix:after {
  display: table;
  content: '';
}
.clearfix:after {
  clear: both;
}
.box-card {
  width: 100%;
  background-color: rgba(242, 242, 242, 0.6);
}
.el-form {
  width: 500px;
}
</style>
